const query = require('../db');
const util = require('../util');
let Class={
    class_name:null,
    creater:null,
    create_time:null,
    desc:null
};
module.exports = {
    //创建班级
    addClass (data,callback) {
        Class=data;
        Class.create_time=new Date().toLocaleString();
        let sql = `INSERT INTO class (class_name,creater,\`desc\`,create_time) VALUES ('${Class.class_name}','${Class.creater}','${Class.desc}','${Class.create_time}')`;
        query(sql,(err,data)=>{
            if(err){
                console.log(err);
            }else{
                callback(data);
            }
        })
    },
    // 获取班级详情
    getClassList(classGuid,callback){
        let class_id = classGuid;
        console.log(2);
        if(class_id){
            let sql = `SELECT * FROM class WHERE class_id = '${class_id}'`;
            query(sql,(err,data)=>{
                if(err){
                    console.log(err);
                }else{
                    callback(data);
                }
            })
        }else{
             let sql = `SELECT * FROM class where status=1`;
             query(sql,(err,data)=>{
                 if(err){
                     console.log(err);
                 }else{
                     callback(data);
                 }
             })
        }
    },
    //获取学生列表
    getSudentList(classGuid,callback){
        let fk_classId = classGuid;
        let sql = `SELECT * FROM user WHERE fk_classId = '${fk_classId}' AND user_type='学生'`;
        query(sql,(err,data)=>{
            if(err){
                console.log(err);
            }else{
                callback(data);
            }
        })
    },
    //根据创建者ID获取班级
    getClassListByUser(user_number,status,callback){
        if(status){
            status=1;
        }else {
            status=0;
        }
        let sql = `SELECT * FROM class WHERE creater = '${user_number}' AND \`status\`=${status}`;
        query(sql,(err,data)=>{
            if(err){
                console.log(err);
            }else {
                callback(data);
            }
        })
    },
    //删除班级
    deleteClassList(classId,callback){
       let sql = `UPDATE class SET status=0 WHERE class_id=${classId};`;
       query(sql,(err,data)=>{
           if(err){
               console.log(err);
           }else {
               callback(data);
           }
       })
    },
    //修改班级
    modeifyClass(data,callback){
        let  sql = `UPDATE class SET class_name='${data.class_name}',\`desc\`='${data.desc}' WHERE class_id=${data.classId}`;
        query(sql,(err,data)=>{
            if(err){
                console.log(err);
            }else {
                callback(data);
            }
        })
    }
};
